package cn._2dland.uploader;

import cn._2dland.uploader.utils.Logger;
import cn._2dland.uploader.utils.UploadUtils;

public class UploadHandler implements IProgressHandler {
	/** 开始时间 */
	private long startTime;

	public UploadHandler() {
		startTime = System.currentTimeMillis();
	}

	public void update(long value, long total) {
		// 计算上传百分比
		double percent = (double)value / total;
		// 计算上传速度，精确到字节
		int speed = Math.round(value / (System.currentTimeMillis() - startTime) * 1000);
		// 计算剩余时间
		long remain = Math.round((double)(total - value) / speed);

		// 构造信息
		StringBuffer titleBuf = new StringBuffer();
		titleBuf.append("上传进度: ").append(UploadUtils.formatPercent(percent));
		titleBuf.append(" / 平均速度: ").append(UploadUtils.formatSpeed(speed));
		titleBuf.append(" / 剩余时间: ").append(UploadUtils.formatTime(remain));

		// 输出到控制台
		Logger.rewritec(titleBuf.toString());
	}

	public void finish() {
		System.out.println();
	}
}
